package gunfire.relics;

import basemod.abstracts.CustomRelic;
import basemod.abstracts.CustomSavable;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.ApplyPowerAction;
import com.megacrit.cardcrawl.actions.common.DamageAction;
import com.megacrit.cardcrawl.actions.common.MakeTempCardInHandAction;
import com.megacrit.cardcrawl.actions.common.RelicAboveCreatureAction;
import com.megacrit.cardcrawl.actions.unique.AddCardToDeckAction;
import com.megacrit.cardcrawl.cards.DamageInfo;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.helpers.FontHelper;
import com.megacrit.cardcrawl.helpers.MathHelper;
import com.megacrit.cardcrawl.helpers.PowerTip;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.powers.DexterityPower;
import com.megacrit.cardcrawl.relics.AbstractRelic;
import com.megacrit.cardcrawl.rooms.AbstractRoom;
import gunfire.GunfireMod;
import gunfire.cards.StoneSkin;
import gunfire.util.TextureLoader;

import static gunfire.GunfireMod.makeRelicOutlinePath;
import static gunfire.GunfireMod.makeRelicPath;

public class SoulOfIron extends CustomRelic implements CustomSavable<int[]> {

    public static final String ID = GunfireMod.makeID("SoulOfIron");
    private static final Texture IMG = TextureLoader.getTexture(makeRelicPath("SoulOfIron.png"));
    private static final Texture OUTLINE = TextureLoader.getTexture(makeRelicOutlinePath("blank.png"));
    public static final int P1 = 250; //回血
    public static final int P2 = 600; //格挡
    public int progress1;
    public int progress2;
    public float offsetX = 0;

    public SoulOfIron() {
        super(ID, IMG, OUTLINE, RelicTier.SPECIAL, LandingSound.CLINK);
        this.progress1 = 0;
        this.progress2 = 0;
    }

    @Override
    public String getUpdatedDescription() {
        return DESCRIPTIONS[0] + DESCRIPTIONS[1] + P1 + DESCRIPTIONS[2] + this.progress1 + " / " + P1 + DESCRIPTIONS[3] + P2 + DESCRIPTIONS[4] + this.progress2 + " / " + P2;
    }

    @Override
    public void obtain() {
        if (AbstractDungeon.player.hasRelic("gunfire:SoulOfTaizi")) {
            for(int i = 0; i < AbstractDungeon.player.relics.size(); ++i) {
                if (AbstractDungeon.player.relics.get(i).relicId.equals(SoulOfTaizi.ID)) {
                    this.instantObtain(AbstractDungeon.player, i, true);
                    break;
                }
            }
        } else {
            super.obtain();
        }
    }

    @Override
    public void atPreBattle() {
        this.refreshDesc();
    }

    @Override
    public void onEquip() {
        if(AbstractDungeon.getCurrRoom().phase == AbstractRoom.RoomPhase.COMBAT){
            this.flash();
            this.addToBot(new ApplyPowerAction(AbstractDungeon.player, AbstractDungeon.player, new DexterityPower(AbstractDungeon.player, 2), 2));
            this.addToBot(new RelicAboveCreatureAction(AbstractDungeon.player, this));
            this.addToBot(new MakeTempCardInHandAction(new StoneSkin(), 1, false));
            this.addToBot(new MakeTempCardInHandAction(new StoneSkin(), 1, false));
        }
        this.addToBot(new AddCardToDeckAction(new StoneSkin()));
        this.addToBot(new AddCardToDeckAction(new StoneSkin()));
    }

    @Override
    public void atBattleStart() {
        this.flash();
        this.addToBot(new ApplyPowerAction(AbstractDungeon.player, AbstractDungeon.player, new DexterityPower(AbstractDungeon.player, 2), 2));
        this.addToBot(new RelicAboveCreatureAction(AbstractDungeon.player, this));
    }

    @Override
    public void onPlayerEndTurn() {
        int damage = (int)(AbstractDungeon.player.currentHealth * 0.1F);
        this.flash();
        this.addToBot(new AbstractGameAction() {
            @Override
            public void update() {
                AbstractMonster m = AbstractDungeon.getRandomMonster();
                //这里必须是top，否则下一个目标可能会选到被第一下打死的
                this.addToTop(new DamageAction(m, new DamageInfo(null, damage, DamageInfo.DamageType.THORNS), AttackEffect.BLUNT_LIGHT));
                this.isDone = true;
            }
        });
        this.addToBot(new AbstractGameAction() {
            @Override
            public void update() {
                AbstractMonster m = AbstractDungeon.getRandomMonster();
                this.addToTop(new DamageAction(m, new DamageInfo(null, damage, DamageInfo.DamageType.THORNS), AttackEffect.BLUNT_LIGHT));
                this.isDone = true;
            }
        });
    }

    @Override
    public int onPlayerHeal(int healAmount) {
        AbstractPlayer p = AbstractDungeon.player;
        int realHeal = Math.min(p.maxHealth - p.currentHealth, healAmount);
        this.progress1 += realHeal;
        this.judgeEvolve();
        return super.onPlayerHeal(healAmount);
    }

    @Override
    public int onPlayerGainedBlock(float blockAmount) {
        this.progress2 += blockAmount;
        this.judgeEvolve();
        return super.onPlayerGainedBlock(blockAmount);
    }

    private void refreshDesc(){
        this.description = DESCRIPTIONS[0] + DESCRIPTIONS[1] + P1 + DESCRIPTIONS[2] + this.progress1 + " / " + P1 + DESCRIPTIONS[3] + P2 + DESCRIPTIONS[4] + this.progress2 + " / " + P2;
        this.tips.clear();
        this.tips.add(new PowerTip(this.name, this.description));
        this.initializeTips();
    }

    private void getOffsetX(){
        float target = (float)(-relicPage * Settings.WIDTH) + (float)relicPage * (PAD_X + 36.0F * Settings.xScale);
        if (AbstractDungeon.player.relics.size() >= MAX_RELICS_PER_PAGE + 1) {
            target += 36.0F * Settings.scale;
        }
        if (this.offsetX != target) {
            this.offsetX = MathHelper.uiLerpSnap(offsetX, target);
        }
    }

    @Override
    public void renderCounter(SpriteBatch sb, boolean inTopPanel) {
        this.getOffsetX();
        if (inTopPanel) {
            FontHelper.renderFontRightTopAligned(sb, FontHelper.tipBodyFont, this.progress1 + "/" + P1, this.offsetX + this.currentX + 30.0F * Settings.scale, this.currentY - 20.0F * Settings.scale, Color.WHITE);
            FontHelper.renderFontRightTopAligned(sb, FontHelper.tipBodyFont, this.progress2 + "/" + P2, this.offsetX + this.currentX + 30.0F * Settings.scale, this.currentY - 40.0F * Settings.scale, Color.WHITE);
        } else {
            FontHelper.renderFontRightTopAligned(sb, FontHelper.tipBodyFont, this.progress1 + "/" + P1, this.currentX + 30.0F * Settings.scale, this.currentY - 20.0F * Settings.scale, Color.WHITE);
            FontHelper.renderFontRightTopAligned(sb, FontHelper.tipBodyFont, this.progress2 + "/" + P2, this.currentX + 30.0F * Settings.scale, this.currentY - 40.0F * Settings.scale, Color.WHITE);
        }
    }

    public void judgeEvolve(){
        if(this.progress1 >= P1){
            new SoulOfIron1().obtain();
        } else if(this.progress2 >= P2){
            new SoulOfIron2().obtain();
        } else {
            this.refreshDesc();
        }
    }

    @Override
    public AbstractRelic makeCopy() {
        return new SoulOfIron();
    }

    @Override
    public int[] onSave() {
        return new int[]{this.progress1, this.progress2};
    }

    @Override
    public void onLoad(int[] p) {
        this.progress1 = p[0];
        this.progress2 = p[1];
        this.refreshDesc();
    }
}
